package com.vip.userregist.action;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.dolphin.component.tools.lang.StringUtil;
import net.dolphin.component.tools.util.CollectUtil;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionRedirect;

import com.vip.buycart.entity.User;
import com.vip.common.UserConstant;
import com.vip.framework.generic.BaseDispatchAction;
import com.vip.framework.generic.GenericBl;

/**
 * 登录Action
 * 
 * @author sai
 *
 */
public class LoginAction extends BaseDispatchAction {

	/**
	 * 用户登录简单实现
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return 用户所在的上一个页面
	 * @throws Exception
	 */
	public ActionForward enter(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		String name=request.getParameter("name");
		String password=request.getParameter("password");
		
		log.info("name is "+name);
		log.info("password is "+password);
		
		String refer = request.getHeader("referer");
		log.info(refer);
		
        ActionRedirect redirect=new ActionRedirect();
        redirect.setPath(refer);
		
		//验证name和password
		if (StringUtil.nullOrEmpty(name) || StringUtil.nullOrEmpty(password)) {
			log.info("not name");
			return redirect;
		}
		
		GenericBl<User> userBl=(GenericBl<User>)getBean("loginBl");
		HashMap<String,String> param = new HashMap<String,String>();
		param.put("name", name);
		
		List<User> users = userBl.search(param, 1, 1);
		if(CollectUtil.isNullOrEmpty(users)){
			log.info("用户不存在");
			return redirect;
		}else if(users.size()==1){
			log.info("size is 1");
			if (users.get(0).getPwd().equals(password))
				 request.getSession().setAttribute(UserConstant.SESSION_LOGIN, users.get(0));
		}else{
			log.error("用户唯一性错误,用户名:"+name);
			return redirect;
		}
		
        return redirect;
		
	}

	
}
